﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Model;
using System.Web.Script.Serialization;
using BLL;
namespace stock
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          //  MoniJob();

        }
        private void MoniJob()
        {
            BLL.WeatherTool wTool = new BLL.WeatherTool();
            List<WeatherAlert> alertList = wTool.GetAllWeatherAlerts();
            if (alertList != null && alertList.Count > 0)
            {
                BLL.CitysBLL cityBL = new BLL.CitysBLL();
                string data = "";
                System.Collections.Hashtable HashWeather = new System.Collections.Hashtable();
                foreach (WeatherAlert alertModel in alertList)
                {
                    Citys cityModel = cityBL.GetModel(alertModel.CityId);
                    if (cityModel != null)
                    {
                        String strUrl = "http://m.weather.com.cn/data/" + cityModel.Number + ".html";
                        Uri uri = new Uri(strUrl);
                        System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
                        System.Net.WebResponse resp = req.GetResponse();
                        System.IO.Stream str3 = resp.GetResponseStream();
                        System.IO.StreamReader readers = new System.IO.StreamReader(str3, System.Text.Encoding.UTF8);
                        data = readers.ReadToEnd();
                        readers.Close();
                        resp.Close();
                        JavaScriptSerializer json = new JavaScriptSerializer();
                        WeatherVirtual weatherTemp = json.Deserialize<WeatherVirtual>(data);
                        HashWeather.Add(alertModel.CityId, weatherTemp);
                    }
                }
                List<WeatherAlert> userAlertModels = new List<WeatherAlert>();
                BLL.WeatherAlertBLL alertBL = new BLL.WeatherAlertBLL();
                userAlertModels = alertBL.GetModelList("");
                Customer user;
                String body;
                BLL.CustomerBLL userBL = new BLL.CustomerBLL();
                foreach (WeatherAlert aModel in userAlertModels)
                {//todo:doit
                    user = userBL.GetModel(aModel.CustomerId);
                    WeatherVirtual temp = (WeatherVirtual)HashWeather[aModel.CityId];
                    body = temp.weatherinfo.city + "," + temp.weatherinfo.date_y + "," + temp.weatherinfo.week + "----今天："
                        + temp.weatherinfo.weather1 + "," + temp.weatherinfo.temp1 + "," + temp.weatherinfo.wind1 + ",48小时穿衣指数：" + temp.weatherinfo.index48_d
                        + ";明天：" + temp.weatherinfo.weather2 + "," + temp.weatherinfo.temp2 + "," + temp.weatherinfo.wind2
                        + ";后天：" + temp.weatherinfo.weather3 + "," + temp.weatherinfo.temp3 + "," + temp.weatherinfo.wind3
                        + ";大后天：" + temp.weatherinfo.weather4 + "," + temp.weatherinfo.temp4 + "," + temp.weatherinfo.wind4;
                    BLL.Tools.SendMail(user.Email, body, "新情况", null, false);
                }
            }

        }
    }
}